package presentation;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;


/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class SearchMenu extends javax.swing.JDialog {
	private static final long serialVersionUID = 1L;
	private JTabbedPane SearchTabbedPane;
	private JPanel byAuthor;
	private JLabel authorLabel;
	private JButton AuthorOk;
	private JLabel dateFromLebel;
	private JTextField dateFrom;
	private JLabel dateToLabel;
	private JButton dateCencel;
	private JTextArea contentText;
	private JButton contentCencel;
	private JButton contentOK;
	private JLabel contentLabel;
	private JTextField dateTo;
	private JButton dateOK;
	private JButton authorCencel;
	private JTextField authorTextFeild;
	private JPanel byDate;
	private JPanel byContant;

	public SearchMenu(JFrame frame) {
		super(frame);
		initGUI();
	}

	private void initGUI() {
		try {
			this.setTitle("Search..");

			SearchTabbedPane = new JTabbedPane();
			getContentPane().add(SearchTabbedPane, BorderLayout.CENTER);
			SearchTabbedPane.setPreferredSize(new java.awt.Dimension(387, 240));
			{
				byAuthor = new JPanel();
				GridBagLayout byAuthorLayout = new GridBagLayout();
				SearchTabbedPane.addTab("By Author", null, getByAuthor(), null);
				byAuthor.setPreferredSize(new java.awt.Dimension(413, 273));
				byAuthorLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.1 };
				byAuthorLayout.rowHeights = new int[] { 64, 30, 60, 28, 7 };
				byAuthorLayout.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.1 };
				byAuthorLayout.columnWidths = new int[] { 7, 99, 83, 46, 75, 7 };
				byAuthor.setLayout(byAuthorLayout);
				{
					authorLabel = new JLabel();
					byAuthor.add(authorLabel, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					authorLabel.setText("Author Name:");
					authorLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
				}
				{
					authorTextFeild = new JTextField();
					byAuthor.add(getAuthorTextFeild(), new GridBagConstraints(2, 1, 3, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					AuthorOk = new JButton();
					byAuthor.add(getAuthoeOk(), new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					AuthorOk.setText("Search");
					AuthorOk.setFont(new java.awt.Font("Tahoma", 1, 12));
					AuthorOk.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							okAuthor();
						}
					});
				}
				{
					authorCencel = new JButton();
					byAuthor.add(getAuthorCencel(), new GridBagConstraints(4, 3, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					authorCencel.setText("Cencel");
					authorCencel.setFont(new java.awt.Font("Tahoma", 1, 12));
					authorCencel.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent arg0) {
							close();
						}
					});
				}
			}
			{
				byDate = new JPanel();
				GridBagLayout byDateLayout1 = new GridBagLayout();
				SearchTabbedPane.addTab("By Date", null, getByDate(), null);
				byDateLayout1.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1 };
				byDateLayout1.rowHeights = new int[] { 38, 28, 24, 28, 41, 28, 7 };
				byDateLayout1.columnWeights = new double[] { 0.0, 0.1, 0.0, 0.0, 0.0, 0.1 };
				byDateLayout1.columnWidths = new int[] { 7, 7, 89, 44, 85, 7 };
				byDate.setLayout(byDateLayout1);
				{
					dateOK = new JButton();
					byDate.add(getDateOK(), new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					dateOK.setText("Search");
					dateOK.setFont(new java.awt.Font("Tahoma", 1, 12));
					dateOK.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							okDate();
						}
					});
				}
				{
					dateCencel = new JButton();
					byDate.add(getDateCencel(), new GridBagConstraints(4, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					dateCencel.setText("Cencel");
					dateCencel.setFont(new java.awt.Font("Tahoma", 1, 12));
					dateCencel.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent arg0) {
							close();
						}
					});
				}
				{
					dateFromLebel = new JLabel();
					byDate.add(dateFromLebel, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					dateFromLebel.setText("From:");
					dateFromLebel.setFont(new java.awt.Font("Tahoma", 1, 14));
					dateFromLebel.setHorizontalAlignment(SwingConstants.CENTER);
				}
				{
					dateToLabel = new JLabel();
					byDate.add(dateToLabel, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					dateToLabel.setText("To:");
					dateToLabel.setHorizontalAlignment(SwingConstants.CENTER);
					dateToLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
				}
				{
					dateFrom = new JTextField();
					byDate.add(getDateFrom(), new GridBagConstraints(2, 1, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					dateFrom.setText("dd/MM/yy");
					dateFrom.setFont(new java.awt.Font("Tahoma", 0, 12));
				}
				{
					dateTo = new JTextField();
					byDate.add(getDateTo(), new GridBagConstraints(2, 3, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					dateTo.setText("dd/MM/yy");
					dateTo.setFont(new java.awt.Font("Tahoma", 0, 12));
				}
			}

			{
				byContant = new JPanel();
				GridBagLayout byContantLayout = new GridBagLayout();
				SearchTabbedPane.addTab("By Content", null, getByContant(), null);
				byContantLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.1 };
				byContantLayout.rowHeights = new int[] { 37, 31, 52, 26, 28, 7 };
				byContantLayout.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.1 };
				byContantLayout.columnWidths = new int[] { 7, 83, 89, 53, 81, 7 };
				byContant.setLayout(byContantLayout);
				{
					contentLabel = new JLabel();
					byContant.add(contentLabel, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					contentLabel.setText("Content:");
					contentLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
					contentLabel.setHorizontalAlignment(SwingConstants.CENTER);
				}
				{
					contentOK = new JButton();
					byContant.add(getContentOK(), new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					contentOK.setText("Search");
					contentOK.setFont(new java.awt.Font("Tahoma", 1, 12));
					contentOK.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e) {
							okContent();
						}

					});
				}
				{
					contentCencel = new JButton();
					byContant.add(getContentCencel(), new GridBagConstraints(4, 4, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					contentCencel.setText("Cencel");
					contentCencel.setFont(new java.awt.Font("Tahoma", 1, 12));
					contentCencel.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent arg0) {
							close();
						}
					});
				}
				{
					contentText = new JTextArea();
					byContant.add(getContentText(), new GridBagConstraints(2, 1, 3, 2, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					contentText.setAutoscrolls(false);
				}
			}

			pack();
			setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * enable my owner (meaning - the JFrame that is passed to the Constructor)
	 * and close myself
	 */
	private void close() {
		this.getParent().setEnabled(true);
		this.dispose();
	}

	private void okDate() {
		String from = this.dateFrom.getText();
		String until = this.dateTo.getText();
		if (from.length() > 0 && until.length() > 0 &&
			!from.equals("dd/MM/yy")&& !until.equals("dd/MM/yy")) {
			WsepGUI tGUI = (WsepGUI) this.getParent();
			tGUI.handleSearchByDate(from, until);
			this.close();
		} else {
			JOptionPane.showMessageDialog(this, "Wrong date format", "Error!", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void okAuthor() {
		String author = this.authorTextFeild.getText();
		if (author.length() > 0) {
			WsepGUI tGUI = (WsepGUI) this.getParent();
			tGUI.handleSearchByAuthor(author);
			this.close();
		} else {
			JOptionPane.showMessageDialog(this, "Missing Data", "Error!", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void okContent() {
		String content = this.contentText.getText();
		if (content.length() > 0) {
			WsepGUI tGUI = (WsepGUI) this.getParent();
			tGUI.handleSearchByContent(content);
			this.close();
		} else {
			JOptionPane.showMessageDialog(this, "Missing Data", "Error!", JOptionPane.ERROR_MESSAGE);
		}
	}

	private Component getByContant() {
		return byContant;
	}

	private Component getByDate() {
		return byDate;
	}

	private Component getByAuthor() {
		return byAuthor;
	}

	public JTextField getAuthorTextFeild() {
		return authorTextFeild;
	}

	public JButton getAuthoeOk() {
		return AuthorOk;
	}

	public JButton getAuthorCencel() {
		return authorCencel;
	}

	public JButton getDateOK() {
		return dateOK;
	}

	public JButton getDateCencel() {
		return dateCencel;
	}

	public JTextField getDateFrom() {
		return dateFrom;
	}

	public JTextField getDateTo() {
		return dateTo;
	}

	public JButton getContentOK() {
		return contentOK;
	}

	public JButton getContentCencel() {
		return contentCencel;
	}

	public JTextArea getContentText() {
		return contentText;
	}

}
